import * as types from '../contants/ActionTypes'
import {WEBSOCKET_CONNECT} from '@giantmachines/redux-websocket'

export const loginSuccess = accountNumber =>({
    type:types.LOGIN_SUCCESS,
    accountNumber
})

export const registerSuccess = () =>({
    type:types.REGISTER_SUCCESS
})

export const registerFail =() =>({
    type:types.REGISTER_FAIL
})
export const refreshFriendList = (friendList) =>({
    type:types.REFRESH_FRIEND_LIST,
    friendList
})


export const sendMessage = (text) =>({
    type:types.SEND_MESSAGE,
    text
})

export const fetchStarted = () => ({
    type:types.FETCH_START
})

export const loginFetchSuccess = (result) => ({
    type:types.LOGIN_FETCH_SUCESS,
    result
})

export const fetchError = (error) => ({
    type:types.FETCH_ERROR,
    error
})

export const setIsOpenChat = (isOpen) =>({
    type:types.SET_IS_OPEN_CHAT,
    isOpen
})

export const matchSuccess = () => ({
    type:types.MATCH_SUCCESS
})

export const matchFail = (messages) =>({
    type:types.MATCH_FAIL,
    messages
})

export const matchStateReset = () => ({
    type:types.MATCH_STATE_RESET
})

export const startFriendChat = (currChatFriendId) =>(
    {
    type:types.START_FRIEND_CHAT,
    currChatFriendId,
})

export const fetchLoginFailed = () =>({
    type:types.LOGIN_FETCH_FAIL
})

export const deleteSuccess = (friendList) =>({
    type:types.DELETE_SUCCESS,
    friendList
})

export const fetchMessageSuccess = (message) =>({
    type:types.FETCH_MESSAGES_SUCCESS,
    message
})


//当action是函数而不是对象时，就会被中间件thunk拦截执行
export const fetchAction = (url) =>{
    return (dispatch) =>{
        dispatch(loginFetchSuccess(null));
        return;
        dispatch(fetchStarted());
        fetch(url).then((resp)=>{
             if(resp.status !== 200) throw new Error('statusError : '+resp.status)//因为fetch不会检查http是否正确
                resp.json().then((respJson) =>{
                    console.log("respJson")
                    console.log(respJson)
              if(respJson.code == 0)
                {console.log("success")
                    dispatch(loginFetchSuccess(respJson.data));}//.info和后台配合一下
              if(respJson.code == 10)
                {   console.log("fail")
                    dispatch(fetchLoginFailed());}
              }).catch((error)=>{
             throw new Error ('Invalid Json resp'+error);
            })
            }).catch((error)=>{
               dispatch(fetchError(error));
           })
     }
}



export const matchStart = (url) =>{
    return (dispatch) =>{
        dispatch(matchSuccess());
        return;
        let respJson ={
            state:true,
            info:{
                matchSuccess:true
            },
            messages:"等待超时"
        }
       // fetch(url).then((resp)=>{
        //    if(resp.status !== 200) throw new Error('statusError : '+resp.status)//因为fetch不会检查http是否正确
         //   resp.json().then((respJson) =>{
                //respJson取到了
                if(respJson.state == true)//匹配成功
                    dispatch(matchSuccess());//在loginReduce里面把isMatch改成true
                else//匹配失败
                    dispatch(matchFail(respJson.messages));
       //     }).catch((error)=>{
     //          throw new Error ('Invalid Json resp'+error);
     //      })
     //   }).catch((error)=>{
    //        dispatch(fetchError(error));
    //    })
    }
}

export const deleteFriend = (url) =>{
    return(dispatch) =>{
        dispatch(deleteSuccess(null));
        return;
        fetch(url).then((resp)=>{
            if(resp.status !== 200) throw new Error('statusError : '+resp.status)//因为fetch不会检查http是否正确
            resp.json().then((respJson) =>{
                //respJson取到了
                console.log(respJson)
                dispatch(deleteSuccess(respJson.data));//在loginReduce里面把isMatch改成true
              }).catch((error)=>{
                  throw new Error ('Invalid Json resp'+error);
             })
         }).catch((error)=>{
                dispatch(fetchError(error));
          })
}
}

export const registerAccount = (url) =>{
    return(dispatch)=>{
        dispatch(registerSuccess());
        return;
        fetch(url).then((resp)=>{
            if(resp.status !== 200) throw new Error('statusError : '+resp.status)//因为fetch不会检查http是否正确
               resp.json().then((respJson) =>{
             if(respJson.code == 11)
               {console.log("success")
                   dispatch(registerSuccess());}//.info和后台配合一下
             if(respJson.code == 1)
               {   console.log("fail")
                   dispatch(registerFail());}
             }).catch((error)=>{
            throw new Error ('Invalid Json resp'+error);
           })
           }).catch((error)=>{
              dispatch(fetchError(error));
          })
    }
}


export const fetchFriendMessage = (url) =>{
    return(dispatch)=>{
        dispatch(fetchMessageSuccess(null));
        return;
        fetch(url).then((resp)=>{
            if(resp.status !== 200) throw new Error('statusError : '+resp.status)//因为fetch不会检查http是否正确
               resp.json().then((respJson) =>{
                   console.log("respJson")
                   console.log(respJson)
                   dispatch(fetchMessageSuccess(respJson.data));//.info和后台配合一下
             }).catch((error)=>{
            throw new Error ('Invalid Json resp'+error);
           })
           }).catch((error)=>{
              dispatch(fetchError(error));
          })
    }
}

export const sendChatMessage = (url) =>{
    return(dispatch)=>{
        return;
        fetch(url).then((resp)=>{
               resp.json().then((respJson) =>{
             })
           })
         }
    }
